const dbconfig = require('../db/dbconfig.js');
const md5 = require('md5-node');
// 用户的模糊查询
searchUser = (qurey) =>{
    let sql  =  `SELECT * FROM user WHERE username LIKE '%${qurey}%' 
                                       or nickName LIKE '%${qurey}%'
                                       or email LIKE '%${qurey}%'
                                       or phone LIKE '%${qurey}%'`
                                  
    return new Promise((resolve,reject)=>{
        dbconfig.query(sql,null,function(result){
          if(result.length > 0)
          resolve(result);
          else
            reject(false);
        })
    })
 }
 
 // 删除用户数据
 removeUser = (qurey) =>{
     let sql = `delete from user where username='${qurey}'`;
     return new Promise((resolve,reject)=>{
         dbconfig.query(sql,null,function(result){
             if(result.affectedRows > 0){
                 resolve(result);
             }
             else
                 reject(false);
         })
     })
     
     
 }
 // 添加用户
 addUser = (data)=>{
     data.password = md5(data.password);
     let sql = `insert into user(username,password) values(?,?)`;
     let arr = [data.username,data.password];
     console.log(arr);
     return new Promise((resovle,reject)=>{
         dbconfig.query(sql,arr,function(result){
             console.log(result);
             if(result.affectedRows > 0){
                 resovle(true)
             }
             else
                reject(false)
         })
     })
 }
//  更新用户信息
editUser = (data)=>{

    
    let sql =`UPDATE user SET nickName=?, 
                                   age=?,
                                   sex=?,
                                 email=?,
                                 phone=?,
                                 avatarUrl=?
              where uid=?`
    let arr = [data.nickName,data.age,data.sex,data.email,data.phone,data.avatarUrl,data.uid];
    return new Promise((resolve,reject)=>{
        dbconfig.query(sql,arr,function(result) {
             if(result.affectedRows > 0){
                resolve(true)
             }
             else
                 reject(false)
        })
    })
}
// 修改密码
editPassword = (newPassword,uid)=>{
    let sql =`UPDATE user SET password='${newPassword}' where uid=${uid}`;
    return new Promise((resolve,reject)=>{
        dbconfig.query(sql,null,function(result){
            if(result.affectedRows > 0){
                resolve('密码修改成功')
            }else{
                reject('修改失败')
            }
        })
    })

}
// 修改密码（管理员
editAdminPassword = (newPassword,id)=>{
    let sql =`UPDATE admin SET password='${newPassword}' where id=${id}`;
    return new Promise((resolve,reject)=>{
        dbconfig.query(sql,null,function(result){
            if(result.affectedRows > 0){
                resolve('密码修改成功')
            }else{
                reject('修改失败')
            }
        })
    })
}
 module.exports = {
    searchUser,
    removeUser,
    addUser,
    editUser,
    editPassword,
    editAdminPassword
 }